﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Support.DTO;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess
{
    public class PatologiaDAL : BaseSQLDAL
    {
        public List<PatologiaDTO> GetPatologias()
        {
            List<PatologiaDTO> listaPatologias = new List<PatologiaDTO>();

            try
            {

                CreateCommand("Patologias_Listado", true);

                ExecuteReader();
                while (Read())
                {
                    listaPatologias.Add(FillDTO(GetDataReader()));
                }
                return listaPatologias;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        private PatologiaDTO FillDTO(SqlDataReader reader)
        {
            int id = reader.GetInt32(reader.GetOrdinal("PatologiaId"));
            string name = reader.GetString(reader.GetOrdinal("Nombre"));
            string desc = reader.GetString(reader.GetOrdinal("Descripcion"));
            return new PatologiaDTO()
            {
                Id = id,
                Nombre = name,
                Descripcion = desc
            };
        }

        public int GuardarPatologia(PatologiaDTO patologia)
        {
            try
            {
                CreateCommand("Patologia_Insertar", true);
                AddCmdParameter("@Nombre", string.IsNullOrEmpty(patologia.Nombre) ? "" : patologia.Nombre, ParameterDirection.Input);
                AddCmdParameter("@Descripcion", string.IsNullOrEmpty(patologia.Descripcion) ? "" : patologia.Descripcion, ParameterDirection.Input);

                ExecuteReader();
                Read();

                return Convert.ToInt32(GetOutResult("patologia_id"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                //DisposeDataReader();
                CloseConnection();
            }
        }

        public int RegistrarEstadistica(int patologiaId)
        {
            try
            {
                CreateCommand("EstadisticaPatologia_Insertar", true);
                AddCmdParameter("@PatologiaId", patologiaId, ParameterDirection.Input);
                AddCmdParameter("@Fecha", DateTime.Now, ParameterDirection.Input);

                ExecuteReader();
                Read();

                return Convert.ToInt32(GetOutResult("estadistica_id"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                //DisposeDataReader();
                CloseConnection();
            }
        }
    }
}
